package com.infinitegroup.findmystuff.activities;

import com.infinitegroup.findmystuff.R;
import com.infinitegroup.findmystuff.presenters.AddItemPresenter;
import com.infinitegroup.findmystuff.views.AddItemView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class AddItemActivity extends Activity implements AddItemView{
	
	private AddItemPresenter presenter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_additem);
		
		Bundle extras = getIntent().getExtras();
		String username = extras.getString("username");
		presenter = new AddItemPresenter(this, username); // pass in the username received from home activity.
		
		Button addItem = (Button) findViewById(R.id.addactivity_addbutton);
		
		addItem.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String itemname = ((EditText)findViewById(R.id.addactivity_itemname)).getText().toString();
				String location = ((EditText)findViewById(R.id.addactivity_location)).getText().toString();
				String reward = ((EditText)findViewById(R.id.addactivity_reward)).getText().toString();
				String description = ((EditText)findViewById(R.id.addactivity_description)).getText().toString();
				String category = ((EditText)findViewById(R.id.additem_category)).getText().toString();
				
				String status = "";
				RadioButton found = (RadioButton) findViewById(R.id.additem_radio_found);
				RadioButton lost = (RadioButton) findViewById(R.id.additem_radio_lost);
				if (found.isChecked()){
					status = "found";
				} else if (lost.isChecked()){
					status = "lost";
				} else {
					displayMessage("Please specify if you've found or lost this item");
				}
				
				presenter.handleAddItemClick(itemname, location, reward, description, category, status);
								
			}
		});
		
		
		Button cancel = (Button) findViewById(R.id.addnewitem_cancelbutton);
		cancel.setOnClickListener( new OnClickListener() {
		@Override
		public void onClick(View v) {
			presenter.handleCancelClick();
		}
	});
	}

	@Override
	public void advanceToHome(String username) {
		Intent intent = new Intent(this, HomeActivity.class);
		intent.putExtra("username", username);
		startActivity(intent);
	}

	@Override
	public void displayMessage(String message) {
		Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}
	
	@Override
	public void end(){
		finish();
	}

}
